package com.stars.module.user.controller;

import com.alibaba.fastjson2.JSONArray;
import com.stars.base.entity.CommonDTO;
import com.stars.base.entity.Result;
import com.stars.module.user.entity.StarOrgUser;
import com.stars.module.user.entity.StarUser;
import com.stars.module.user.service.StarUserService;
import com.stars.util.common.DevelopKit;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author : Rocks
 * @version : 1.0
 * @date : 2022/11/22 23:50
 * @description : 用户控制器
 */
@RestController
@RequestMapping("/starUser")
public class StarUserController {

    private final StarUserService starUserService;

    public StarUserController(StarUserService starUserService) {
        this.starUserService = starUserService;
    }

    @GetMapping("/getUserInfo")
    public Result getUserInfo(){
        return Result.success(DevelopKit.getSessionUser());
    }

    @PostMapping("/loadUserByDeptId")
    public Result loadUserByDeptId(@RequestBody CommonDTO dto){
        return Result.success(starUserService.loadUserByDeptId(dto));
    }

    @PostMapping("/loadUserByRoleId")
    public Result loadUserByRoleId(@RequestBody CommonDTO dto){
        return Result.success(starUserService.loadUserByRoleId(dto));
    }

    @GetMapping("/loadUserById")
    public Result loadUserById(String id){
        return Result.success(starUserService.loadUserById(id));
    }

    @PostMapping("/saveUser")
    public Result saveUser(@RequestBody StarUser user){
        return starUserService.saveOrUpdateUser(user)?Result.success():Result.error();
    }

    /**
     * 角色配置人员
     * @param starOrgUser 角色配置人员
     */
    @PostMapping("/saveOrgUser")
    public Result saveOrgUser(@RequestBody StarOrgUser starOrgUser){
        return starUserService.saveOrgUser(starOrgUser)?Result.success():Result.error();
    }

    @PostMapping("removeUser")
    public Result removeUser(String uid,String deptId){
        return starUserService.removeUser(uid,deptId)?Result.success():Result.error();
    }

    @RequestMapping("/getMaxIndex")
    public Result getMaxIndex (String fid) {
        return Result.success(starUserService.getMaxIndex(fid));
    }

    /**
     * 设置兼职部门/主部门
     * @return
     */
    @RequestMapping("/putPartTimeDept")
    @Transactional
    public Result putPartTimeDept(String uid, String deptIds, Integer isMain){
        return Result.success(starUserService.putPartTimeDept(uid,deptIds,isMain));
    }

    /**
     * 用户部门关系列表
     * @return
     */
    @RequestMapping("/loadUserPartTimeDept")
    public Result loadUserPartTimeDept(@RequestBody CommonDTO dto){
        return Result.success(starUserService.loadUserPartTimeDept(dto));
    }
}
